As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!" The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences. For example, given the following spreadsheet: 5 1 9 5 7 5 3 2 4 6 8 The first row's largest and smallest values are 9 and 1, and their difference is 8. The second row's largest and smallest values are 7 and 3, and their difference is 4. The third row's difference is 6. In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. What is the checksum for the spreadsheet in your puzzle input?

In [63]:
spreadsheet = readdlm("inputs/day2.txt", Int32)


Out[63]:
16×16 Array{Int32,2}:
 1364   461  1438  1456   818   999  …  1353   148   837   590   404   123
  204    99   235  2281  2848  3307     3525   525   288   278  3059   821
  280   311   100   287   265   383      398    99   194   297   399    87
 7698  2334  7693   218  7344  3887     7412  6147   231  1066   248   208
 3740   837  4144   123   155  2494     1221  4061    95   148  3460   550
 1376  1462    73   968    95  1721  …  1683   618    82  1660    83  1778
  197  2295  5475  2886  2646   186     1477   196  1778  3496  5041  3314
  179  2949  3197  2745  1341  3128     2692   212  2487  2947  3547  1120
  460    73    52   373    41   133      715   644   182   524   648   320
  169   207  5529  4820   248  6210     5472  3954  3791  1311  7074  5729
 5965  7445  2317   196  1886  3638  …   229   230  1791  6900  3108  5827
  212   249   226   129   196   245      184    99   276    93   222    56
   51   592   426    66   594   406      522    57   547    65   564   622
  215  2092  1603  1001   940  2054     2808   208   194  2339  2028  2580
  378   171   155  1100   184   937     1611  1349   647  1778  1723  1709
 4463  4757   201   186  3812  2413  …  2898   200  5536  5226  1028   180

In [64]:
@time sum(maximum(spreadsheet, 2) - minimum(spreadsheet, 2))


  0.234362 seconds (126.68 k allocations: 5.911 MiB)
Out[64]:
53460
--- Part Two --- "Great work; looks like we're on the right track after all. Here's a star for your effort." However, the program seems a little worried. Can programs be worried? "Based on what we're seeing, it looks like all the User wanted is some information about the evenly divisible values in the spreadsheet. Unfortunately, none of us are equipped for that kind of calculation - most of us specialize in bitwise operations." It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result. For example, given the following spreadsheet: 5 9 2 8 9 4 7 3 3 8 6 5 In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4. In the second row, the two numbers are 9 and 3; the result is 3. In the third row, the result is 2. In this example, the sum of the results would be 4 + 3 + 2 = 9. What is the sum of each row's result in your puzzle input?

In [92]:
function evenly_divide(row)
    L = length(row)
    row = sort(row)
    for i = 1:L
        for j = (i+1):L
            r = row[j] // row[i]
            if r.den == 1
                return r.num
            end
        end
    end
end


Out[92]:
evenly_divide (generic function with 1 method)

In [93]:
a = [5 9 2 8; 9 4 7 3; 3 8 6 5]


Out[93]:
3×4 Array{Int64,2}:
 5  9  2  8
 9  4  7  3
 3  8  6  5

In [94]:
sum(mapslices(evenly_divide, a, 2))


Out[94]:
9

In [96]:
@time sum(mapslices(evenly_divide, spreadsheet, 2))


  0.000997 seconds (1.33 k allocations: 27.094 KiB)
Out[96]:
282

In [ ]: